package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.PermissionQueryDO;
import com.recharge.domain.query.RoleQueryDO;
import com.recharge.domain.vo.PermissionDO;
import com.recharge.domain.vo.RoleDO;
import com.recharge.domain.vo.UserLoginDO;
import com.recharge.service.PermissionService;
import com.recharge.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 权利 前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/permission")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;

    @RequiresPermissions("permission:add")
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    @ApiOperation(value="增加权利信息", notes="")
    public @ResponseBody
    ObjectResponse<PermissionDO> add(@RequestBody PermissionDO domain) {

        ObjectResponse<PermissionDO> response = new ObjectResponse<>();
        domain.setCreator(getUserName());
        ResultDO result = permissionService.add(domain);

        if(result.isSuccess()) {
            response.setData((PermissionDO)result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("permission:edit")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    @ApiOperation(value="编辑权利信息", notes="")
    public @ResponseBody
    ObjectResponse<RoleDO> edit(@RequestBody PermissionDO domain) {

        ObjectResponse<RoleDO> response = new ObjectResponse<>();
        domain.setModifier(getUserName());
        ResultDO result = permissionService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("permission:get")
    @RequestMapping(value="/get.html", method = RequestMethod.POST)
    @ApiOperation(value="获取权利信息", notes="")
    public @ResponseBody
    ObjectResponse<PermissionDO> get(@RequestBody PermissionDO domain) {

        ObjectResponse<PermissionDO> response = new ObjectResponse<>();

        ResultDO result = permissionService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PermissionDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("permission:delete")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    @ApiOperation(value="删除权利信息", notes="")
    public @ResponseBody
    ObjectResponse delete(@RequestBody PermissionDO domain) {

        ObjectResponse response = new ObjectResponse();
        UserLoginDO userLoginDO = getUserLoginInfo();

        ResultDO result = permissionService.remove(domain.getId(), userLoginDO.getUsername());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("permission:tree")
    @RequestMapping(value="/tree.html", method = RequestMethod.POST)
    @ApiOperation(value="权利树", notes="")
    public @ResponseBody  ObjectResponse tree(@RequestBody PermissionQueryDO query) {

        ObjectResponse response = new ObjectResponse<>();

        ResultDO result = permissionService.getTree(query);

        if(result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }

        return response;
    }
}
